/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JInternalFrameEditPassword.java
 *
 * Created on Dec 15, 2011, 2:35:38 PM
 */
package bugreportingsystem;

import Util.CommonQuery;
import Util.Employee;
import java.awt.Color;
import java.awt.Font;
import java.util.Vector;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

/**
 *
 * @author PhucTakua
 */
public class JInternalFrameChangePassword extends javax.swing.JInternalFrame {

    /** Creates new form JInternalFrameEditPassword */
    int ide = JFrameLogin.idemployee;
    private String language = JFrameLogin.language;
    private String country = JFrameLogin.country;
    private Locale lc = new Locale(language, country);
    private ResourceBundle rb = ResourceBundle.getBundle("bugreportingsystem/resources/JInternalFrameChangePassword", lc);

    public JInternalFrameChangePassword() {
        initComponents();
        MyTranslate();
    }

    private TitledBorder SetTitleBorder(String a) {
        Border etched = BorderFactory.createEtchedBorder();
        Font font = new Font("Tahoma", Font.BOLD, 14);
        TitledBorder Title = BorderFactory.createTitledBorder(etched, rb.getString(a));
        Title.setTitleJustification(TitledBorder.LEFT);
        Title.setTitleFont(font);
        Title.setTitleColor(Color.getColor("BLUE", 204));
        return Title;
    }

    private void MyTranslate() {

        jLabel1.setText(rb.getString("jLabel1.text"));
        jLabel2.setText(rb.getString("jLabel2.text"));
        jLabel3.setText(rb.getString("jLabel3.text"));
        jButtonSave.setText(rb.getString("jButtonSave.text"));
        jButtonCancel.setText(rb.getString("jButtonCancel.text"));
        jPanel5.setBorder(SetTitleBorder("jPanel5.border.title"));
        this.setTitle(rb.getString("Form.title"));
    }

    public static String convertPassword(char[] cPassword) {
        // Declare variables
        String strRet = new String("");

        // Go through each character
        for (int i = 0; i < cPassword.length; i++) {
            strRet += cPassword[i];
        }

        return strRet;
    }

    private String EncryptionPassword(String pass) {

        CommonQuery com = new CommonQuery();
        String password = "";

        if (CommonQuery.CheckString(pass)) {
            password = com.Encrypt(pass, ide);
        }
        return password;
    }

    private void EncryptionOldPassword() {
        Vector<Employee> liside = Employee.ListIDE();
        CommonQuery com = new CommonQuery();
        for (Employee e : liside) {
            if (ide == e.getIdemployee()) {
                String a = com.Encrypt(e.getPassword(), ide);
                e.setIdemployee(ide);
                e.setPassword(a);
                e.ChangePassword();
                break;
            }
        }
    }

    private void DEncryptionPassword() {
        String oldpassword = "";
        CommonQuery com = new CommonQuery();
        Vector<Employee> listid = Employee.ListIDE();
        for (Employee e : listid) {
            if (e.getIdemployee() == ide) {
                oldpassword = com.DEncrypt(e.getPassword(), ide);
                e.setIdemployee(ide);
                e.setPassword(oldpassword);
                e.ChangePassword();
            }
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        jPanel18 = new javax.swing.JPanel();
        jPanel19 = new javax.swing.JPanel();
        jLabeloldpass = new javax.swing.JLabel();
        jLabelnewpass = new javax.swing.JLabel();
        jLabelconfirmpass = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        jPanel16 = new javax.swing.JPanel();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        jPasswordFieldOldPassword = new javax.swing.JPasswordField();
        jPasswordFieldNewpassword = new javax.swing.JPasswordField();
        jPasswordFieldConfirmNewpassword = new javax.swing.JPasswordField();
        jPanel13 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bugreportingsystem.BugReportingSystemApp.class).getContext().getResourceMap(JInternalFrameChangePassword.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(1018, 607));

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(853, 85));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 908, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 85, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(220, 85));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 220, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 219, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.LINE_START);

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(270, 85));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel17.setName("jPanel17"); // NOI18N
        jPanel17.setPreferredSize(new java.awt.Dimension(235, 45));

        javax.swing.GroupLayout jPanel17Layout = new javax.swing.GroupLayout(jPanel17);
        jPanel17.setLayout(jPanel17Layout);
        jPanel17Layout.setHorizontalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
        );
        jPanel17Layout.setVerticalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 45, Short.MAX_VALUE)
        );

        jPanel3.add(jPanel17, java.awt.BorderLayout.PAGE_START);

        jPanel18.setName("jPanel18"); // NOI18N
        jPanel18.setPreferredSize(new java.awt.Dimension(235, 75));

        javax.swing.GroupLayout jPanel18Layout = new javax.swing.GroupLayout(jPanel18);
        jPanel18.setLayout(jPanel18Layout);
        jPanel18Layout.setHorizontalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
        );
        jPanel18Layout.setVerticalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 75, Short.MAX_VALUE)
        );

        jPanel3.add(jPanel18, java.awt.BorderLayout.PAGE_END);

        jPanel19.setName("jPanel19"); // NOI18N
        jPanel19.setLayout(new java.awt.GridLayout(3, 1, 0, 10));

        jLabeloldpass.setFont(resourceMap.getFont("jLabeloldpass.font")); // NOI18N
        jLabeloldpass.setForeground(resourceMap.getColor("jLabeloldpass.foreground")); // NOI18N
        jLabeloldpass.setName("jLabeloldpass"); // NOI18N
        jPanel19.add(jLabeloldpass);

        jLabelnewpass.setFont(resourceMap.getFont("jLabelnewpass.font")); // NOI18N
        jLabelnewpass.setForeground(resourceMap.getColor("jLabelnewpass.foreground")); // NOI18N
        jLabelnewpass.setName("jLabelnewpass"); // NOI18N
        jPanel19.add(jLabelnewpass);

        jLabelconfirmpass.setFont(resourceMap.getFont("jLabelconfirmpass.font")); // NOI18N
        jLabelconfirmpass.setForeground(resourceMap.getColor("jLabelconfirmpass.foreground")); // NOI18N
        jLabelconfirmpass.setName("jLabelconfirmpass"); // NOI18N
        jPanel19.add(jLabelconfirmpass);

        jPanel3.add(jPanel19, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel3, java.awt.BorderLayout.LINE_END);

        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setPreferredSize(new java.awt.Dimension(648, 230));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 908, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 230, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), resourceMap.getString("jPanel5.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel5.border.titleFont"), resourceMap.getColor("jPanel5.border.titleColor"))); // NOI18N
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setLayout(new java.awt.BorderLayout(0, 10));

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setPreferredSize(new java.awt.Dimension(448, 15));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 15, Short.MAX_VALUE)
        );

        jPanel5.add(jPanel6, java.awt.BorderLayout.PAGE_START);

        jPanel7.setMinimumSize(new java.awt.Dimension(100, 30));
        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(448, 15));
        jPanel7.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel7, java.awt.BorderLayout.PAGE_END);

        jPanel8.setName("jPanel8"); // NOI18N
        jPanel8.setLayout(new java.awt.BorderLayout(15, 10));

        jPanel9.setName("jPanel9"); // NOI18N
        jPanel9.setPreferredSize(new java.awt.Dimension(301, 35));
        jPanel9.setLayout(new java.awt.BorderLayout());

        jPanel15.setName("jPanel15"); // NOI18N
        jPanel15.setPreferredSize(new java.awt.Dimension(150, 35));

        javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
        jPanel15.setLayout(jPanel15Layout);
        jPanel15Layout.setHorizontalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jPanel15Layout.setVerticalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 35, Short.MAX_VALUE)
        );

        jPanel9.add(jPanel15, java.awt.BorderLayout.LINE_START);

        jPanel16.setName("jPanel16"); // NOI18N
        jPanel16.setLayout(new java.awt.GridLayout(1, 2, 20, 0));

        jButtonSave.setIcon(resourceMap.getIcon("jButtonSave.icon")); // NOI18N
        jButtonSave.setText(resourceMap.getString("jButtonSave.text")); // NOI18N
        jButtonSave.setName("jButtonSave"); // NOI18N
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });
        jPanel16.add(jButtonSave);

        jButtonCancel.setIcon(resourceMap.getIcon("jButtonCancel.icon")); // NOI18N
        jButtonCancel.setText(resourceMap.getString("jButtonCancel.text")); // NOI18N
        jButtonCancel.setName("jButtonCancel"); // NOI18N
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        jPanel16.add(jButtonCancel);

        jPanel9.add(jPanel16, java.awt.BorderLayout.CENTER);

        jPanel8.add(jPanel9, java.awt.BorderLayout.PAGE_END);

        jPanel10.setName("jPanel10"); // NOI18N
        jPanel10.setLayout(new java.awt.BorderLayout(20, 0));

        jPanel11.setName("jPanel11"); // NOI18N
        jPanel11.setLayout(new java.awt.GridLayout(3, 1, 0, 10));

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel11.add(jLabel1);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel11.add(jLabel2);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel11.add(jLabel3);

        jPanel10.add(jPanel11, java.awt.BorderLayout.LINE_START);

        jPanel12.setName("jPanel12"); // NOI18N
        jPanel12.setLayout(new java.awt.GridLayout(3, 1, 0, 10));

        jPasswordFieldOldPassword.setText(resourceMap.getString("jPasswordFieldOldPassword.text")); // NOI18N
        jPasswordFieldOldPassword.setName("jPasswordFieldOldPassword"); // NOI18N
        jPanel12.add(jPasswordFieldOldPassword);

        jPasswordFieldNewpassword.setText(resourceMap.getString("jPasswordFieldNewpassword.text")); // NOI18N
        jPasswordFieldNewpassword.setName("jPasswordFieldNewpassword"); // NOI18N
        jPanel12.add(jPasswordFieldNewpassword);

        jPasswordFieldConfirmNewpassword.setText(resourceMap.getString("jPasswordFieldConfirmNewpassword.text")); // NOI18N
        jPasswordFieldConfirmNewpassword.setName("jPasswordFieldConfirmNewpassword"); // NOI18N
        jPanel12.add(jPasswordFieldConfirmNewpassword);

        jPanel10.add(jPanel12, java.awt.BorderLayout.CENTER);

        jPanel8.add(jPanel10, java.awt.BorderLayout.CENTER);

        jPanel5.add(jPanel8, java.awt.BorderLayout.CENTER);

        jPanel13.setName("jPanel13"); // NOI18N
        jPanel13.setPreferredSize(new java.awt.Dimension(20, 104));
        jPanel13.setRequestFocusEnabled(false);

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        jPanel5.add(jPanel13, java.awt.BorderLayout.LINE_END);

        jPanel14.setName("jPanel14"); // NOI18N
        jPanel14.setPreferredSize(new java.awt.Dimension(20, 104));

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        jPanel5.add(jPanel14, java.awt.BorderLayout.LINE_START);

        getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed


        DEncryptionPassword();
        Vector<Employee> liside = Employee.ListIDE();
        String oldpassword = convertPassword(this.jPasswordFieldOldPassword.getPassword());
        String newpassword = convertPassword(this.jPasswordFieldNewpassword.getPassword());
        String confirmnewpassword = convertPassword(this.jPasswordFieldConfirmNewpassword.getPassword());


        boolean kq = true;
        if (!CommonQuery.CheckString(oldpassword)) {
            this.jLabeloldpass.setText(rb.getString("OldPass"));
            this.jLabelnewpass.setText("");
            this.jLabelconfirmpass.setText("");
            EncryptionOldPassword();
        } else if (!CommonQuery.CheckString(newpassword)) {
            this.jLabeloldpass.setText("");
            this.jLabelconfirmpass.setText("");
            this.jLabelnewpass.setText(rb.getString("NewPass"));
            EncryptionOldPassword();
        } else if (!newpassword.equals(confirmnewpassword)) {
            this.jLabeloldpass.setText("");
            this.jLabelconfirmpass.setText(rb.getString("NewOldPass"));
            this.jLabelnewpass.setText("");
            EncryptionOldPassword();
        } else {
            for (Employee e : liside) {
                if (!oldpassword.equals(e.getPassword().toString()) || ide != e.getIdemployee()) {
                    kq = false;
                } else {
                    Employee em = new Employee();
                    em.setIdemployee(ide);
                    em.setPassword(EncryptionPassword(newpassword));
                    if (em.ChangePassword()) {
                        JOptionPane.showMessageDialog(null, rb.getString("ChangeSuccess"));
                        this.jPasswordFieldConfirmNewpassword.setText("");
                        this.jPasswordFieldNewpassword.setText("");
                        this.jPasswordFieldOldPassword.setText("");
                        jLabeloldpass.setText("");
                        jLabelnewpass.setText("");
                        jLabelconfirmpass.setText("");
                    } else {
                        JOptionPane.showMessageDialog(null, rb.getString("ChangeFail"));
                        EncryptionOldPassword();
                    }
                    kq = true;
                    break;
                }

            }
            if (!kq) {
                this.jLabeloldpass.setText(rb.getString("InputOldPassIncorect"));
                this.jLabelnewpass.setText("");
                this.jLabelconfirmpass.setText("");
                EncryptionOldPassword();
            }
        }

    }//GEN-LAST:event_jButtonSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelconfirmpass;
    private javax.swing.JLabel jLabelnewpass;
    private javax.swing.JLabel jLabeloldpass;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPasswordField jPasswordFieldConfirmNewpassword;
    private javax.swing.JPasswordField jPasswordFieldNewpassword;
    private javax.swing.JPasswordField jPasswordFieldOldPassword;
    // End of variables declaration//GEN-END:variables
}
